/*
 * Copyright 2015 Eduard Ereza Martínez
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mm.dss.demo;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;

import com.android.business.adapter.DataAdapterImpl;
import com.android.business.exception.BusinessException;
import com.blankj.utilcode.util.Utils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.view.CropImageView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.mm.dss.demo.tools.PicassoImageLoader;
//import com.tencent.smtt.sdk.JsContext;
import com.tencent.smtt.sdk.QbSdk;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class DSSApplication extends MultiDexApplication {
    private static DSSApplication instance;

    public static synchronized DSSApplication getApplication() {
        if (instance == null) {
            throw new NullPointerException("app not create or be terminated!");
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        QbSdk.initX5Environment(this,null);//注册腾讯TBs浏览服务
        ExceptionHandler.getInstance().initConfig(this);
        instance = this;
        try {
            DataAdapterImpl.getInstance().createDataAdapter("com.android.business.adapter.DataAdapteeImpl");
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        loadLibrary();
        Utils.init(this);
        initOkGo();

        try {
            init();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  /*  @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }*/

    private void loadLibrary(){
        System.loadLibrary("gnustl_shared");
        System.loadLibrary("CommonSDK");
        System.loadLibrary("Encrypt");
        System.loadLibrary("DPSStream");
        System.loadLibrary("dsl");
        System.loadLibrary("dslalien");
        System.loadLibrary("PlatformSDK");
        System.loadLibrary("DPRTSPSDK");
    }
    //限制字体尺寸，避免受系统字体大小影响导致界面不适配
    @Override
    public Resources getResources() {
        Resources resources = super.getResources();
        Configuration configuration = new Configuration();
        configuration.setToDefaults();
        resources.updateConfiguration(configuration,resources.getDisplayMetrics());
        return resources;
    }

    private void init() throws Exception{
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new PicassoImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);  //显示拍照按钮
        imagePicker.setCrop(true);        //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true); //是否按矩形区域保存
        imagePicker.setSelectLimit(9);    //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);//保存文件的高度。单位像素

    }
    private void initOkGo() {
        //自己写的配置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.readTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);          //全局的读取超时时间
        builder.writeTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);         //全局的写入超时时间
        builder.connectTimeout(OkGo.DEFAULT_MILLISECONDS, TimeUnit.MILLISECONDS);       //全局的连接超时时间
        OkGo.getInstance().init(this)                                                    //必须调用初始化
                .setOkHttpClient(builder.build())                                       //建议设置OkHttpClient，不设置将使用默认的
                .setCacheMode(CacheMode.NO_CACHE)                                       //全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)                              //全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(3);                   //全局统一超时重连次数，默认为三次，那么最差的情况会请求4次(一次原始请求，三次重连请求)，不需要可以设置为0
    }
    @Override
    public String getPackageName() {
        return super.getPackageName();
    }
}
